package com.example.library.controller;

import com.example.library.entity.User;
import com.example.library.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/readers")
public class ReaderController {
    @Autowired
    private ReaderService readerService;

    @GetMapping
    public List<User> list() {
        return readerService.findAllReaders();
    }

    @PutMapping("/{id}")
    public User update(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        return readerService.save(user);
    }

    @PostMapping
    public User create(@RequestBody User user) {
        return readerService.save(user);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        readerService.deleteById(id);
    }
}
